package com.lefull.lefullbase.dialog;


import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.ContextThemeWrapper;
import android.widget.ImageView;
import com.lefull.lefullbase.R;
import com.lefull.lefullbase.base.LeFullBaseModule;

import java.io.IOException;
import pl.droidsonroids.gif.GifDrawable;

public class ImageDialog extends Dialog {

    private String msg;
    private Context context;
    private static ImageDialog dialog;
    ImageView ivLoading;
    private GifDrawable gifDrawable = null;

    public static synchronized void showDialog(Context context, String msg) {
        if (dialog == null) {
            dialog = new ImageDialog(context, msg);
        }
        if (!dialog.isShowing()) {
            if (context != null && context instanceof Activity) {
                if (!((Activity) context).isFinishing()) {
                    dialog.show();
                }
            } else {
                dialog.show();
            }
        }

    }

    public static synchronized void showDialog(Context context) {
        if (!LeFullBaseModule.isNetwork) {
            return;
        }
        if (dialog == null) {
            dialog = new ImageDialog(context);
        }
        if (!dialog.isShowing()) {
            if (context != null && context instanceof Activity) {
                if (!((Activity) context).isFinishing()) {
                    dialog.show();
                }
            }
        }

    }

    public static synchronized void closeDialog(Context context) {
        try {
            if (dialog != null && dialog.isShowing() && context != null && context instanceof Activity && !((Activity) context).isFinishing()) {
                dialog.dismiss();
                dialog = null;
            }
        } catch (final IllegalArgumentException e) {
            // Handle or log or ignore
        } catch (final Exception e) {
            // Handle or log or ignore
        } finally {
            if (dialog != null && dialog.isShowing() && context != null && context instanceof Activity && !((Activity) context).isFinishing()) {
                dialog.dismiss();
                dialog = null;
            }
        }
    }

    public ImageDialog(Context context, String msg) {
        super(context, R.style.dialog);
        // setCancelable(false);
        setCanceledOnTouchOutside(false);
        this.context = context;
        this.msg = msg;
    }

    public ImageDialog(Context context) {
        super(context, R.style.dialog);
        // setCancelable(false);
        setCanceledOnTouchOutside(false);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loading);
        ivLoading = findViewById(R.id.iv_loading);
    }


    @Override
    public void show() {
        if (context instanceof Activity) {
            if (!((Activity) context).isFinishing()) {
                super.show();
                try {
                    gifDrawable = new GifDrawable(context.getResources(), R.drawable.loading);
                    // gif1加载一个动态图gif
                    ivLoading.setImageDrawable(gifDrawable);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        } else if (context instanceof ContextThemeWrapper) {
            if (context != null) {
                super.show();

            }
        }

    }

    @Override
    public void dismiss() {
        super.dismiss();
        dialog = null;
    }
}
